import { earthBranches, relation } from "./consts";
import { fiveElements } from "./enums";

export default function changeMean(
  mainSixMembers,
  changeSixMembers,
  symbols
) {
  const branches = getEffectBranches(mainSixMembers, changeSixMembers, symbols);

  for (let i = 0; i < branches.length; i++) {
    mainSixMembers[branches[i].index].changeMean = "";
    if (反吟(branches[i])) {
      mainSixMembers[branches[i].index].changeMean += "反吟";
      // continue;
    }
    if (伏吟(branches[i])) {
      mainSixMembers[branches[i].index].changeMean += "伏吟";
      // continue;
    }
    if (回头克(branches[i])) {
      mainSixMembers[branches[i].index].changeMean += "回头克";
      // continue;
    }
    if (回头生(branches[i])) {
      mainSixMembers[branches[i].index].changeMean += "回头生";
      // continue;
    }
    if (化进(branches[i])) {
      mainSixMembers[branches[i].index].changeMean = "化进";
      // continue;
    }
    if (化退(branches[i])) {
      mainSixMembers[branches[i].index].changeMean += "化退";
      // continue;
    }
    if (化绝(branches[i])) {
      mainSixMembers[branches[i].index].changeMean += "化绝";
      // continue;
    }
    if (化长生(branches[i])) {
      mainSixMembers[branches[i].index].changeMean += "化长生";
      // continue;
    }
    if (化墓(branches[i])) {
      mainSixMembers[branches[i].index].changeMean += "化墓";
      // continue;
    }
    if (化合(branches[i])) {
      mainSixMembers[branches[i].index].changeMean += "化合绊";
      // continue;
    }
  }
}

function getEffectBranches(mainSixMembers, changeSixMembers, symbols) {
  const ret = [];
  for (let i = 0; i < symbols.length; i++) {
    if (symbols[i] == 6 || symbols[i] == 9) {
        ret.push({
          main: mainSixMembers[i].earthBranch,
          change: changeSixMembers[i].earthBranch,
          index: i
        });
      
    }
  }
  return ret;
}

function 回头克({ main, change }) {
  switch (main.element) {
    case fiveElements.土:
      if (change.element == fiveElements.木) return true;
      break;
    case fiveElements.木:
      if (change.element == fiveElements.金) return true;
      break;
    case fiveElements.水:
      if (change.element == fiveElements.土) return true;
      break;
    case fiveElements.火:
      if (change.element == fiveElements.水) return true;
      break;
    case fiveElements.金:
      if (change.element == fiveElements.火) return true;
      break;
  }
  return false;
}

function 回头生({ main, change }) {
  switch (main.element) {
    case fiveElements.土:
      if (change.element == fiveElements.火) return true;
      break;
    case fiveElements.木:
      if (change.element == fiveElements.水) return true;
      break;
    case fiveElements.水:
      if (change.element == fiveElements.金) return true;
      break;
    case fiveElements.火:
      if (change.element == fiveElements.木) return true;
      break;
    case fiveElements.金:
      if (change.element == fiveElements.土) return true;
      break;
  }
  return false;
}

function 反吟({ main, change }) {
  if (main != change) {
    const factors = relation.impact.find(
      (x) => x.factors.indexOf(main) > -1
    ).factors;
    if (factors.indexOf(change) > -1) return true;
  }
  return false;
}

function 伏吟({ main, change }) {
  return main == change;
}

function 化进({ main, change }) {
  switch (main) {
    case earthBranches.亥:
      if (change == earthBranches.子) return true;
      break;
    case earthBranches.寅:
      if (change == earthBranches.卯) return true;
      break;
    case earthBranches.巳:
      if (change == earthBranches.午) return true;
      break;
    case earthBranches.申:
      if (change == earthBranches.酉) return true;
      break;
    case earthBranches.丑:
      if (change == earthBranches.辰) return true;
      break;
    case earthBranches.辰:
      if (change == earthBranches.未) return true;
      break;
    case earthBranches.未:
      if (change == earthBranches.戌) return true;
      break;
    case earthBranches.戌:
      if (change == earthBranches.丑) return true;
      break;
  }
  return false;
}
function 化退({ main, change }) {
  switch (main) {
    case earthBranches.子:
      if (change == earthBranches.亥) return true;
      break;
    case earthBranches.卯:
      if (change == earthBranches.寅) return true;
      break;
    case earthBranches.午:
      if (change == earthBranches.巳) return true;
      break;
    case earthBranches.酉:
      if (change == earthBranches.申) return true;
      break;
    case earthBranches.辰:
      if (change == earthBranches.丑) return true;
      break;
    case earthBranches.未:
      if (change == earthBranches.辰) return true;
      break;
    case earthBranches.戌:
      if (change == earthBranches.未) return true;
      break;
    case earthBranches.丑:
      if (change == earthBranches.戌) return true;
      break;
  }
  return false;
}

function 化绝({ main, change }) {
  switch (change) {
    case earthBranches.申:
      if (main.element == fiveElements.木) return true;
      break;
    case earthBranches.亥:
      if (main.element == fiveElements.火) return true;
      break;
    case earthBranches.寅:
      if (main.element == fiveElements.金) return true;
      break;
    case earthBranches.巳:
      if (main.element == fiveElements.水) return true;
      break;
  }
  return false;
}

function 化墓({ main, change }) {
  switch (change) {
    case earthBranches.未:
      if (main.element == fiveElements.木) return true;
      break;
    case earthBranches.戌:
      if (main.element == fiveElements.火) return true;
      break;
    case earthBranches.丑:
      if (main.element == fiveElements.金) return true;
      break;
    case earthBranches.辰:
      if (main.element == fiveElements.水) return true;
      break;
  }
  return false;
}

function 化合({ main, change }) {
  if (main != change) {
    const factors = relation.combine6.find(
      (x) => x.factors.indexOf(main) > -1
    ).factors;
    if (factors.indexOf(change) > -1) return true;
  }
  return false;
}

function 化长生({ main, change }) {
  switch (change) {
    case earthBranches.申:
      if (main.element == fiveElements.水 || main.element == fiveElements.土) return true;
      break;
    case earthBranches.亥:
      if (main.element == fiveElements.木) return true;
      break;
    case earthBranches.寅:
      if (main.element == fiveElements.火) return true;
      break;
    case earthBranches.巳:
      if (main.element == fiveElements.金) return true;
      break;
  }
  return false;
}
